package com.ptbuy.statanalysys;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ptbuy.MainActivity;
import com.ptbuy.R;
import com.ptbuy.db.dao.ClientDao;
import com.ptbuy.db.dao.ProductDao;
import com.ptbuy.db.pojo.ClientPojo;
import com.ptbuy.db.pojo.ProductPojo;
import com.ptbuy.utils.Utils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 统计分析
 */
public class StatanAnalysisActivity extends AppCompatActivity {

    WebView webView;
    private ProductDao productDao;
    private ClientDao clientDao;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.analysis_statan);

        findViewById(R.id.analysisBack).setOnClickListener(v -> back());
        productDao = new ProductDao();
        clientDao = new ClientDao();
        initWebView();

    }

    List<String> data;
    List<String> productPriceData;
    List<String> productData;
    List<DataValuePojo> dataValue;

    @SuppressLint("SetJavaScriptEnabled")
    private void initWebView() {
        webView = findViewById(R.id.webView);
        webView.setWebViewClient(new WebViewClient());
        //此方法可以启用html5页面的javascript
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setGeolocationEnabled(true);
        //自适应屏幕
        webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webView.getSettings().setLoadWithOverviewMode(true);
        //设置可以支持缩放
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.getSettings().setDisplayZoomControls(false);
        webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        String urlPath = "file:///android_asset/analysis/echars.html";
        webView.loadUrl(urlPath);

        //html互动
        initHtml();

    }

    String productPrice = "0";

    //html互动
    private void initHtml() {
        data = new ArrayList<>();
        productData = new ArrayList<>();//品牌
        productPriceData = new ArrayList<>();//价格
        String name = String.valueOf(clientDao.queryCount());
        String product = String.valueOf(clientDao.queryTotalProductInfo());
        List<String> list = clientDao.queryTotalPriceInfo();
        Long total = 0L;
        for (String str : list) {
            total += Long.parseLong(str.replace(",",""));
        }
        String dealPrice = Utils.getNumlStr(total.toString());
        productPrice = dealPrice;
        List<DataValuePojo> dataValuePojos = productDao.queryProductAndPriceo();
        for (DataValuePojo data : dataValuePojos) {
            productData.add(data.getName());
            String value = data.getValue();
            productPriceData.add(Utils.getNumlStr(value));
        }


        data.add("客户");
        data.add("总件数");
//        data.add("总价格");
        data.add("收购人");
        dataValue = new ArrayList<>();
        dataValue.add(new DataValuePojo(name, "客户"));
        dataValue.add(new DataValuePojo(product, "总件数"));
//        dataValue.add(new DataValuePojo(productPrice, "总价格"));
        dataValue.add(new DataValuePojo(name, "收购人"));
        webView.addJavascriptInterface(new JSInterface(), "Android");
    }

    private void back() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            back();
        }
        return false;
    }


    private class JSInterface {

        @JavascriptInterface
        public String getDataSumPrice() {
            // 在这里返回需要传递给 HTML 的参数
            return JSON.toJSONString(productPrice);
        }

        @JavascriptInterface
        public String getData() {
            // 在这里返回需要传递给 HTML 的参数
            return JSON.toJSONString(data);
        }

        @JavascriptInterface
        public String getDataValue() {
            // 在这里返回需要传递给 HTML 的参数
            return JSON.toJSONString(dataValue);
        }

        @JavascriptInterface
        public String getDataZZ() {
            // 在这里返回需要传递给 HTML 的参数
            return JSON.toJSONString(productPriceData);
        }

        @JavascriptInterface
        public String getProductData() {
            // 在这里返回需要传递给 HTML 的参数
            return JSON.toJSONString(productData);
        }

        @JavascriptInterface
        public String getDealData(String name) {
            if ("总件数".equalsIgnoreCase(name)) {
                List<ProductPojo> pojoList = productDao.queryInfo(0, 0, null);

                ProductPojo productPojo = new ProductPojo();
                productPojo.setProductName("合计");
                Long total = 0L;
                for (ProductPojo product : pojoList) {
                    total += Long.parseLong(product.getProductPrice().replace(",", ""));
                    String dealPrice = Utils.getNumlStr(product.getProductPrice());
                    product.setProductPrice(dealPrice);

                    product.setTime(Utils.stringToTime(product.getTime()));

                }

                String dealPrice = Utils.getNumlStr(total.toString());

                productPojo.setProductPrice(dealPrice);

                pojoList.add(pojoList.size(), productPojo);
                return JSONObject.toJSONString(pojoList);
            }

            List<ClientPojo> clientPojos = clientDao.queryRecodInfo(0, 0, null);
            ClientPojo clientPojo = new ClientPojo();
            clientPojo.setAgentName("合计");
            clientPojo.setName("合计");
            Long total = 0L;
            Integer sum = 0;
            for (ClientPojo client : clientPojos) {
                total += Long.parseLong(client.getTotalPrice().replace(",", ""));
                sum += client.getTotalProduct();
                client.setTime(Utils.stringToTime(client.getTime()));
            }
            clientPojos.add(clientPojos.size(), clientPojo);
            String dealPrice = Utils.getNumlStr(total.toString());
            clientPojo.setTotalPrice(dealPrice);
            clientPojo.setTotalProduct(sum);
            return JSONObject.toJSONString(clientPojos);
        }
    }
}